﻿using System;
using System;
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using netDxf;
using netDxf.Blocks;
using netDxf.Collections;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Objects;
using netDxf.Tables;
using netDxf.Units;
using Attribute = netDxf.Entities.Attribute;
using Image = netDxf.Entities.Image;
using Point = netDxf.Entities.Point;
using Trace = netDxf.Entities.Trace;
using System.Threading;

namespace TestDxfDocument
{

    class Colietsfortoolholderswithtaperratio1_104
    {
        public static void _Colietsfortoolholderswithtaperratio1_104(Dictionary<string, Object> data)//D-螺母
        {
            DxfDocument dxf = new DxfDocument();
            //图层
            Layer contour = fun.SetLayer("contour");//轮廓层
            Layer dimensioning = fun.SetLayer("dimensioning");//尺寸层
            Layer refin = fun.SetLayer("refin");//基准线/内轮廓/虚线
            Layer center = fun.SetLayer("center");//中心线
            Layer textlayer = fun.SetLayer("textlayer");//文本层
            Layer hatching = fun.SetLayer("hatching");//填充层
            Layer additionalline = fun.SetLayer("additionalline");//附加线
            Layer cutting = fun.SetLayer("cutting");//切削层




            //参数
            double pi = Math.PI;
            string path = Convert.ToString(data["path"]);

            //double scale = Convert.ToDouble(data["scale"]); //放大系数
            double scale = 5;


            double d5 = Convert.ToDouble(data["d5"]) * scale;
            double d7 = Convert.ToDouble(data["d7"]) * scale;
            double l11 = Convert.ToDouble(data["l11"]) * scale;

            //坐标
            Vector2 v1 = new Vector2(0, 0);
            Vector2 v2 = new Vector2(v1.X + l11 + 4, v1.Y);
            Vector2 v3 = new Vector2(v1.X + 2, v1.Y);
            Vector2 v4 = new Vector2(v3.X, v3.Y + d7 / 2.105);
            Vector2 v5 = new Vector2(v3.X + l11 / 13.99, v3.Y + d7 / 2);
            Vector2 v6 = new Vector2(v3.X + l11 / 1.134, v5.Y);
            Vector2 v7 = new Vector2(v3.X + l11, v3.Y + d7 / 2.238);
            Vector2 v8 = new Vector2(v7.X, v7.Y * -1);
            Vector2 v9 = new Vector2(v6.X, v6.Y * -1);
            Vector2 v10 = new Vector2(v5.X, v5.Y * -1);
            Vector2 v11 = new Vector2(v4.X, v4.Y * -1);
            Vector2 v12 = new Vector2(v5.X + l11 / 7.059, v5.Y);
            Vector2 v13 = new Vector2(v6.X - l11 / 5.829, v12.Y);
            Vector2 v14 = new Vector2(v3.X, v3.Y + d5 / 2);
            Vector2 v15 = new Vector2(v3.X + l11 / 24.822, v3.Y + d5 / 2.071);
            Vector2 v16 = new Vector2(v15.X + l11 / 2.074, v15.Y);
            Vector2 v17 = new Vector2(v16.X + l11 / 17.1, v14.Y);
            Vector2 v18 = new Vector2(v17.X + l11 / 9.74, v17.Y);
            Vector2 v19 = new Vector2(v7.X, v18.Y);
            Vector2 v20 = new Vector2(v14.X, v14.Y * -1);
            Vector2 v21 = new Vector2(v15.X, v15.Y * -1);
            Vector2 v22 = new Vector2(v16.X, v16.Y * -1);
            Vector2 v23 = new Vector2(v17.X, v17.Y * -1);
            Vector2 v24 = new Vector2(v18.X, v18.Y * -1);
            Vector2 v25 = new Vector2(v19.X, v19.Y * -1);
            Vector2 v26 = new Vector2(v12.X, v12.Y * -1);
            Vector2 v27 = new Vector2(v13.X, v13.Y * -1);
            Vector2 v28 = new Vector2(v18.X, v3.Y + d5 / 2.72);
            Vector2 v29 = new Vector2(v28.X + l11 / 15.3, v28.Y);
            Vector2 v30 = new Vector2(v29.X + l11 / 17.895, v29.Y);
            Vector2 v31 = new Vector2(v30.X + l11 / 17.895, v29.Y);
            Vector2 v32 = new Vector2(v19.X, v3.Y + d5 / 3.148);
            Vector2 v33 = new Vector2(v28.X, v28.Y * -1);
            Vector2 v34 = new Vector2(v29.X, v29.Y * -1);
            Vector2 v35 = new Vector2(v30.X, v30.Y * -1);
            Vector2 v36 = new Vector2(v31.X, v31.Y * -1);
            Vector2 v37 = new Vector2(v32.X, v32.Y * -1);
            Vector2 v38 = new Vector2(v18.X + (v19.X - v18.X) / 2, v18.Y);
            Vector2 v39 = new Vector2(v38.X, v38.Y * -1);
            Vector2 v40 = new Vector2(v38.X - l11 / 16.2, v38.Y);
            Vector2 v41 = new Vector2(v38.X + l11 / 16.2, v38.Y);
            Vector2 v42 = new Vector2(v39.X - l11 / 16.2, v39.Y);
            Vector2 v43 = new Vector2(v39.X + l11 / 16.2, v39.Y);

            //作图
            Line line1 = new Line(v1, v2);
            line1.Layer = center;
            line1.Linetype = Linetype.DashDot;
            line1.LinetypeScale = 3;
            dxf.AddEntity(line1);

            LwPolyline line2 = new LwPolyline();
            line2.Vertexes.Add(new LwPolylineVertex(v4));
            line2.Vertexes.Add(new LwPolylineVertex(v5));
            line2.Vertexes.Add(new LwPolylineVertex(v6));
            line2.Vertexes.Add(new LwPolylineVertex(v7));
            line2.Vertexes.Add(new LwPolylineVertex(v8));
            line2.Vertexes.Add(new LwPolylineVertex(v9));
            line2.Vertexes.Add(new LwPolylineVertex(v10));
            line2.Vertexes.Add(new LwPolylineVertex(v11));
            line2.Vertexes.Add(new LwPolylineVertex(v4));
            fun.PolyFill(dxf, line2, 0.5, contour);

            LwPolyline line3 = new LwPolyline();
            line3.Vertexes.Add(new LwPolylineVertex(v14));
            line3.Vertexes.Add(new LwPolylineVertex(v15));
            line3.Vertexes.Add(new LwPolylineVertex(v21));
            line3.Vertexes.Add(new LwPolylineVertex(v20));
            fun.PolyFill(dxf, line3, 0.5, contour);

            LwPolyline line4 = new LwPolyline();
            line4.Vertexes.Add(new LwPolylineVertex(v15));
            line4.Vertexes.Add(new LwPolylineVertex(v16));
            line4.Vertexes.Add(new LwPolylineVertex(v22));
            line4.Vertexes.Add(new LwPolylineVertex(v21));
            fun.PolyFill(dxf, line4, 0.5, contour);

            LwPolyline line5 = new LwPolyline();
            line5.Vertexes.Add(new LwPolylineVertex(v16));
            line5.Vertexes.Add(new LwPolylineVertex(v17));
            line5.Vertexes.Add(new LwPolylineVertex(v23));
            line5.Vertexes.Add(new LwPolylineVertex(v22));
            fun.PolyFill(dxf, line5, 0.5, contour);

            LwPolyline line6 = new LwPolyline();
            line6.Vertexes.Add(new LwPolylineVertex(v17));
            line6.Vertexes.Add(new LwPolylineVertex(v18));
            line6.Vertexes.Add(new LwPolylineVertex(v24));
            line6.Vertexes.Add(new LwPolylineVertex(v23));
            fun.PolyFill(dxf, line6, 0.5, contour);

            LwPolyline line7 = new LwPolyline();
            line7.Vertexes.Add(new LwPolylineVertex(v19));
            line7.Vertexes.Add(new LwPolylineVertex(v18));
            line7.Vertexes.Add(new LwPolylineVertex(v24));
            line7.Vertexes.Add(new LwPolylineVertex(v25));
            //fun.PolyFill(dxf, line7, 0.5, contour);

            LwPolyline line8 = new LwPolyline();
            line8.Vertexes.Add(new LwPolylineVertex(v29));
            line8.Vertexes.Add(new LwPolylineVertex(v30));
            line8.Vertexes.Add(new LwPolylineVertex(v35));
            line8.Vertexes.Add(new LwPolylineVertex(v34));
            line8.Vertexes[0].Bulge = Math.Tan(155 * pi / 180 / 4);
            line8.Vertexes[2].Bulge = Math.Tan(155 * pi / 180 / 4);
            fun.PolyFill(dxf, line8, 0.5, contour);

            LwPolyline line9 = new LwPolyline();
            line9.Vertexes.Add(new LwPolylineVertex(v30));
            line9.Vertexes.Add(new LwPolylineVertex(v31));
            line9.Vertexes.Add(new LwPolylineVertex(v36));
            line9.Vertexes.Add(new LwPolylineVertex(v35));
            fun.PolyFill(dxf, line9, 0.5, contour);

            LwPolyline line10 = new LwPolyline();
            line10.Vertexes.Add(new LwPolylineVertex(v31));
            line10.Vertexes.Add(new LwPolylineVertex(v32));
            line10.Vertexes.Add(new LwPolylineVertex(v37));
            line10.Vertexes.Add(new LwPolylineVertex(v36));
            fun.PolyFill(dxf, line10, 0.5, contour);

            LwPolyline line11 = new LwPolyline();
            line11.Vertexes.Add(new LwPolylineVertex(v28));
            line11.Vertexes.Add(new LwPolylineVertex(v29));
            line11.Vertexes.Add(new LwPolylineVertex(v34));
            line11.Vertexes.Add(new LwPolylineVertex(v33));
            fun.PolyFill(dxf, line11, 0.5, contour);

            LwPolyline line12 = new LwPolyline();
            line12.Vertexes.Add(new LwPolylineVertex(v12));
            line12.Vertexes.Add(new LwPolylineVertex(v13));
            line12.Vertexes[0].Bulge = Math.Tan(96 * pi / 180 / 4);
            fun.PolyFill(dxf, line12, 0.5, contour);

            LwPolyline line13 = new LwPolyline();
            line13.Vertexes.Add(new LwPolylineVertex(v27));
            line13.Vertexes.Add(new LwPolylineVertex(v26));
            line13.Vertexes[0].Bulge = Math.Tan(96 * pi / 180 / 4);
            fun.PolyFill(dxf, line13, 0.5, contour);

            fun.drawcircle(dxf, v38, (v35 - v39).Y / 2, 0.5, contour);

            fun.drawcircle(dxf, v39, (v35 - v39).Y / 2, 0.5, contour);



            //填充
            LwPolyline hatchpoly1 = new LwPolyline();
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v40));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v41));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v19));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v7));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v6));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v13));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v12));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v5));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v4));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v14));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v15));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v16));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v17));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v40));
            hatchpoly1.Vertexes[0].Bulge = Math.Tan(-180 * pi / 180 / 4);
            hatchpoly1.Vertexes[5].Bulge = Math.Tan(-96 * pi / 180 / 4);
            hatchpoly1.IsClosed = true;
            String[] htype1 = new String[100];
            Line[] hline1 = new Line[100];
            Arc[] harc1 = new Arc[100];
            int hnumst1 = hatchpoly1.Explode().Count;  //记录爆炸后，线段有多少
            for (int i = 0; i < hnumst1; i++)
            {
                htype1[i] = hatchpoly1.Explode()[i].Type.ToString();    //将所有的元素记录成一个数组
                if (htype1[i] == "Line")
                {
                    hline1[i] = (Line)hatchpoly1.Explode()[i];
                }
                if (htype1[i] == "Arc")
                {
                    harc1[i] = (Arc)hatchpoly1.Explode()[i];
                }
            }
            fun.hatchsub1(dxf, htype1, hnumst1, 45, 10, hline1, harc1, hatching);

            LwPolyline hatchpoly2 = new LwPolyline();
            hatchpoly2.Vertexes.Add(new LwPolylineVertex(v42));
            hatchpoly2.Vertexes.Add(new LwPolylineVertex(v43));
            hatchpoly2.Vertexes.Add(new LwPolylineVertex(v25));
            hatchpoly2.Vertexes.Add(new LwPolylineVertex(v8));
            hatchpoly2.Vertexes.Add(new LwPolylineVertex(v9));
            hatchpoly2.Vertexes.Add(new LwPolylineVertex(v27));
            hatchpoly2.Vertexes.Add(new LwPolylineVertex(v26));
            hatchpoly2.Vertexes.Add(new LwPolylineVertex(v10));
            hatchpoly2.Vertexes.Add(new LwPolylineVertex(v11));
            hatchpoly2.Vertexes.Add(new LwPolylineVertex(v20));
            hatchpoly2.Vertexes.Add(new LwPolylineVertex(v21));
            hatchpoly2.Vertexes.Add(new LwPolylineVertex(v22));
            hatchpoly2.Vertexes.Add(new LwPolylineVertex(v23));
            hatchpoly2.Vertexes.Add(new LwPolylineVertex(v42));
            hatchpoly2.Vertexes[0].Bulge = Math.Tan(180 * pi / 180 / 4);
            hatchpoly2.Vertexes[5].Bulge = Math.Tan(96 * pi / 180 / 4);
            hatchpoly2.IsClosed = true;
            String[] htype2 = new String[100];
            Line[] hline2 = new Line[100];
            Arc[] harc2 = new Arc[100];
            int hnumst2 = hatchpoly2.Explode().Count;  //记录爆炸后，线段有多
                                                       //
                                                       //少
            for (int i = 0; i < hnumst2; i++)
            {
                htype2[i] = hatchpoly2.Explode()[i].Type.ToString();    //将所有的元素记录成一个数组
                if (htype2[i] == "Line")
                {
                    hline2[i] = (Line)hatchpoly2.Explode()[i];
                }
                if (htype2[i] == "Arc")
                {
                    harc2[i] = (Arc)hatchpoly2.Explode()[i];
                }
            }
            fun.hatchsub1(dxf, htype2, hnumst2, 45, 10, hline2, harc2, hatching);

            LwPolyline hatchpoly3 = new LwPolyline();
            hatchpoly3.Vertexes.Add(new LwPolylineVertex(v40));
            hatchpoly3.Vertexes.Add(new LwPolylineVertex(v41));
            hatchpoly3.Vertexes.Add(new LwPolylineVertex(v19));
            hatchpoly3.Vertexes.Add(new LwPolylineVertex(v32));
            hatchpoly3.Vertexes.Add(new LwPolylineVertex(v31));
            hatchpoly3.Vertexes.Add(new LwPolylineVertex(v30));
            hatchpoly3.Vertexes.Add(new LwPolylineVertex(v29));
            hatchpoly3.Vertexes.Add(new LwPolylineVertex(v28));
            hatchpoly3.Vertexes.Add(new LwPolylineVertex(v18));
            hatchpoly3.Vertexes.Add(new LwPolylineVertex(v40));
            hatchpoly3.Vertexes[0].Bulge = Math.Tan(180 * pi / 180 / 4);
            hatchpoly3.Vertexes[5].Bulge = Math.Tan(-155 * pi / 180 / 4);
            hatchpoly3.IsClosed = true;
            String[] htype3 = new String[100];
            Line[] hline3 = new Line[100];
            Arc[] harc3 = new Arc[100];
            int hnumst3 = hatchpoly3.Explode().Count;  //记录爆炸后，线段有多
                                                       //
                                                       //少
            for (int i = 0; i < hnumst3; i++)
            {
                htype3[i] = hatchpoly3.Explode()[i].Type.ToString();    //将所有的元素记录成一个数组
                if (htype3[i] == "Line")
                {
                    hline3[i] = (Line)hatchpoly3.Explode()[i];
                }
                if (htype3[i] == "Arc")
                {
                    harc3[i] = (Arc)hatchpoly3.Explode()[i];
                }
            }
            fun.hatchsub1(dxf, htype3, hnumst3, 135, 10, hline3, harc3, hatching);

            LwPolyline hatchpoly4 = new LwPolyline();
            hatchpoly4.Vertexes.Add(new LwPolylineVertex(v42));
            hatchpoly4.Vertexes.Add(new LwPolylineVertex(v43));
            hatchpoly4.Vertexes.Add(new LwPolylineVertex(v25));
            hatchpoly4.Vertexes.Add(new LwPolylineVertex(v37));
            hatchpoly4.Vertexes.Add(new LwPolylineVertex(v36));
            hatchpoly4.Vertexes.Add(new LwPolylineVertex(v35));
            hatchpoly4.Vertexes.Add(new LwPolylineVertex(v34));
            hatchpoly4.Vertexes.Add(new LwPolylineVertex(v33));
            hatchpoly4.Vertexes.Add(new LwPolylineVertex(v24));
            hatchpoly4.Vertexes.Add(new LwPolylineVertex(v42));
            hatchpoly4.Vertexes[0].Bulge = Math.Tan(-180 * pi / 180 / 4);
            hatchpoly4.Vertexes[5].Bulge = Math.Tan(155 * pi / 180 / 4);
            hatchpoly4.IsClosed = true;
            String[] htype4 = new String[100];
            Line[] hline4 = new Line[100];
            Arc[] harc4 = new Arc[100];
            int hnumst4 = hatchpoly4.Explode().Count;  //记录爆炸后，线段有多
                                                       //
                                                       //少
            for (int i = 0; i < hnumst4; i++)
            {
                htype4[i] = hatchpoly4.Explode()[i].Type.ToString();    //将所有的元素记录成一个数组
                if (htype4[i] == "Line")
                {
                    hline4[i] = (Line)hatchpoly4.Explode()[i];
                }
                if (htype4[i] == "Arc")
                {
                    harc4[i] = (Arc)hatchpoly4.Explode()[i];
                }
            }
            fun.hatchsub1(dxf, htype4, hnumst4, 135, 10, hline4, harc4, hatching);


            Line linea1 = new Line(v14, v17);
            Line linea2 = new Line(v20, v23);

            Line lc1 = new Line(v24, v25);
            Line lc2 = new Line(Pus.RotateByPoint(v39, 90 * pi / 180, v24, v25)[1], Pus.RotateByPoint(v39, 90 * pi / 180, v24, v25)[2]);

            Line lc3 = fun.mirrorline(lc1, v1, v2);
            Line lc4 = fun.mirrorline(lc2, v1, v2);

            //标注

            double height = 3.5;
            double arrowsize = height;
            double exline = height / 2;
            double offset = 10;



            fun.dimrot1(dxf, v14, v20, 90, offset, 1, height, arrowsize, exline, "d5=%%C" + d5 / scale, "", "");

            fun.dimrot1(dxf, v5, v10, 90, offset * 2 + v5.X - v4.X, 1, height, arrowsize, exline, "d7=%%C" + d7 / scale, "", "");

            fun.dimrot1(dxf, v11, v8, 180, offset + v11.Y - v10.Y, 1, height, arrowsize, exline, "l11=" + l11 / scale, "", "");


            double isframe = Convert.ToInt32(data["isframe"]);
            String isframe_chose = Convert.ToString(data["isframe_chose"]);
            Vector2 p0 = new Vector2(0, 0);
            if (isframe == 1)
            {
                if (isframe_chose.Equals("0"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA0X(dxf, new Vector2(p0.X - 1189 / 2.0, p0.Y + 841 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("1"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA1X(dxf, new Vector2(p0.X - 841 / 2.0, p0.Y + 594 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("2"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA2X(dxf, new Vector2(p0.X - 594 / 2.0, p0.Y + 420 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("3"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA3X(dxf, new Vector2(p0.X - 420 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4X"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4X(dxf, new Vector2(p0.X - 297 / 2.0, p0.Y + 210 / 2.0 - 10 * 2.26), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4Y"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4Y(dxf, new Vector2(p0.X - 210 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }
            }

            fun.AddEntities(dxf, additionalline, linea1, linea2);
            fun.AddEntities(dxf, center, lc1, lc2, lc3, lc4);

            fun.SetLayer(dxf); fun.SetByLayer(dxf);



            //定义格式，路径导出
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);


        }

    }
}

